package com.example.demo38inheritablethreadlocal.thread;

/**
 * InheritableThreadLocal 可以获取父线程 ThreadLocal 的数据
 * 在 Thread#init 方法中，会将父线程的 inheritableThreadLocals 拷贝给新线程，这样就能在子线程中获取到父线程的 ThreadLocal 数据
 */
public class InheritableThreadLocalTest {

    public static void main(String[] args) {
        InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
        inheritableThreadLocal.set("hello");
        System.out.println("threadLocal.get() = " + inheritableThreadLocal.get());
        new Thread(() -> {
            String name = Thread.currentThread().getName();
            System.out.println("name + threadLocal.get() = " + name + ":" + inheritableThreadLocal.get());
        }).start();
    }

}
